Page({
  data: {
    ruleOpen: [false, false],
    couponList: [
      {
        id: 'c1',
        type: 'amount',
        amount: 10,
        unit: '元',
        title: '10元代金券',
        date: '2025-07-02 ~ 2025-07-03',
        rule: {
          id: '8972428731',
          store: '全部门店',
          goods: '全部商品',
          scene: '自提 外卖 扫码点餐 快餐 当面付 快递',
          valid: '获得券当日起开始1个自然日内有效',
          desc: ''
        }
      },
      {
        id: 'c2',
        type: 'discount',
        amount: 9.3,
        unit: '折',
        title: '9.3折扣券',
        date: '2025-07-02 ~ 2025-08-31',
        rule: null
      }
    ]
  },
  goBack() {
    wx.navigateBack();
  },
  toggleRule(e) {
    const idx = e.currentTarget.dataset.idx;
    const arr = this.data.ruleOpen.slice();
    arr[idx] = !arr[idx];
    this.setData({ ruleOpen: arr });
  },
  copyId(e) {
    wx.setClipboardData({ data: e.currentTarget.dataset.id });
  },
  onReceiveCoupon(e) {
    const idx = e.currentTarget.dataset.idx;
    const coupon = this.data.couponList[idx];
    // 保存到本地"我的优惠券"
    let myCoupons = wx.getStorageSync('myCoupons') || [];
    myCoupons.unshift(coupon);
    wx.setStorageSync('myCoupons', myCoupons);
    // 从当前列表移除
    const couponList = this.data.couponList.slice();
    couponList.splice(idx, 1);
    const ruleOpen = this.data.ruleOpen.slice();
    ruleOpen.splice(idx, 1);
    this.setData({ couponList, ruleOpen });
    wx.showToast({ title: '领取成功', icon: 'success' });
  }
}); 